<template v-if="param.portletList">
    <div>
        <grid-layout
            class="layoutContainer"
            :layout.sync="param.portletList"
            :responsive-layouts="param.testLayouts"
            :col-num="12"
            :row-height="param.layoutConfig.rowHeight||5"
            :is-draggable="draggable"
            :is-resizable="resizable"
            :responsive="false"
            :vertical-compact="true"
            :use-css-transforms="true"
            :margin="param.layoutConfig.margin"
        >
            <grid-item
                v-for="item in param.portletList"
                class="grid-item grid-item2"
                :x="item.x"
                :y="item.y"
                :w="item.w"
                :h="item.h"
                :i="item.i"
                :key="item.i"
                @resized="resizedEvent"
                @moved="movedEvent"
            >
                <vue-scroll
                    :ops="ops"
                    class="portlet"
                    :key="item.pindex"
                    @handle-resize="handleResize"
                    @mouseenter.native="addBarListener"
                >
                    <div
                        ref="vs" 
                        :is="item.content.templateName"
                        :content="item.content"
                    ></div>
                    <div class="dragBtn"></div>
                </vue-scroll>
            </grid-item>
        </grid-layout>
    </div>
</template>

<script>
import Vue from 'vue';
import VueGridLayout from '~/static/js/vue-grid-layout.common';

import portlets from '~/components/componentStore/portlets';
// import IframeBox from '../common/iframeBox';
import { createNamespacedHelpers } from 'vuex';
const { mapMutations, mapActions,mapState } = createNamespacedHelpers('homeModule');
 
import appUtil from '~/assets/utils/appUtil.js';
export default {
    name: 'WorkDashboard',
    components: {
        GridLayout: VueGridLayout.GridLayout,
        GridItem: VueGridLayout.GridItem,
        // IframeBox
    },
    props:{
        param:{
            type: Object,
            required: true
        }
    },
    data() {
        return {
            ops: {
                rail: {
                    opacity: '0'
                    //size: '10px'
                },
                bar: {
                    onlyShowBarOnScroll: false,
                    background: '#909399',
                    size: '4px'
                }
            },
            draggable: true,
            resizable: true,
        };
    },
 
    computed: {
        ...mapState({
            currentRoleTheme:state => state.currentRoleTheme
        })
    },
    created () {
        let componentGroup = Object.values(portlets);
        console.log(this.currentRoleTheme);
        componentGroup.forEach(group => {
            group.forEach(component => {
                // installed组件有没有注册过
                if (!component.installed) {
                    Vue.component(component.templateName, component.template);
                    component.installed = true;  // 设置此组件已被注册
                    component.forEdit = true;  // 当前是否在编辑页面使用
                }
            });
        });
        
    },
    methods: {
        ...mapMutations([
            'updatefasklintFlag'
        ]),
        ...mapActions([
            'getDefaultThemeAsync',
        ]),
        // breakpointChangedEvent: function(newBreakpoint, newLayout){
        //     console.log('BREAKPOINT CHANGED breakpoint=', newBreakpoint, ', layout: ', newLayout );
        // },
        resizedEvent() {
            let layout = {
                userId: appUtil.getSes('userId'),
                themeId: this.currentRoleTheme.themeId,
                name:this.currentRoleTheme.themeName,
                innerData: this.param.portletList
            };
            //console.log(val);
            this.$axios
                .post(
                    '/ptp-engine/apis/engTheme/front/1',
                    JSON.stringify(layout),
                    { headers: { 'Content-type': 'application/json' } }
                )
                .then(() => {
                    this.updatefasklintFlag(); // 通知fastlink
                    this.$message.success('个人布局保存成功');
                });
        },
        movedEvent() {
            let layout = {
                userId: appUtil.getSes('userId'),
                themeId: this.currentRoleTheme.themeId,
                name:this.currentRoleTheme.themeName,
                innerData: this.param.portletList
            };
            //console.log(val);
            this.$axios
                .post(
                    '/ptp-engine/apis/engTheme/front/1',
                    JSON.stringify(layout),
                    { headers: { 'Content-type': 'application/json' } }
                )
                .then(() => {
                    this.updatefasklintFlag(); // 通知fastlink
                    this.$message.success('个人布局保存成功');
                });
        },
        handleResize () {

        },
        // 防止部件拖拽事件跟滚动条拖动冲突
        addBarListener() {
            let that = this;
            let bars = process.client ? document.querySelectorAll(
                '.__bar-is-vertical, .__bar-is-horizontal'
            ) : {};
            for (let i = 0; i < bars.length; i++) {
                bars[i].addEventListener('mouseover', function() {
                    that.draggable = false;
                });
                bars[i].addEventListener('mouseout', function() {
                    that.draggable = true;
                });
            }
        }
    },
    
};
</script>
<style scoped>
.layoutContainer{
    padding-bottom: 90px;
    
}
</style>
<style lang="scss">
.__view {
        width: 100% !important;
    }
.el-card{
    border: none !important;
}
.dragBtn{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 10px;
}
.portlet {
    width: 100%;
    height: 100%;

}
.el-scrollbar__wrap.default-scrollbar__wrap {
    overflow-x: auto;
}
.vue-grid-layout {
    cursor: default;
}
.vue-grid-item {
    // background-color: #fff;
    position: relative;
}

.vue-resizable-handle {
    width: 25px;
    height: 25px;
}
.vue-resizable-handle:hover {
    border: 2px solid #555;
    width: 25px;
    height: 25px;
    box-shadow: 0 0 3px 3px #e6e6e6;
    opacity: 0.3;
}
.el-row {
    margin-bottom: 10px;
}
.el-table--small {
    font-size: 14px !important;
}
.el-tabs__item {
    height: 34px;
    line-height: 34px!important;
}
.el-tabs__nav-wrap {
    padding-left: 48px;
}
.el-table::before {
    height: 0;
}
.el-tabs__nav-wrap::after {
    height: 1px;
}
.el-tabs__item {
    color: #576069;
}
.card-box {
    position: relative;
}
.card-box .btn-more {
    position: absolute;
    top: 5px;
    right: 10px;
    font-size: 14px;
    font-weight: 400;
}
.tabs-content-box {
    height:80%!important;
    padding: 0 1% 5px;
    box-sizing:border-box;
}
.todo-tag {
    width: 24px;
    height: 24px;
    background: #a7a7a7;
    border-radius: 4px;
    text-align: center;
    line-height: 24px;
    color: #fff;
}
.todo-tag-1 {
    background: #f25e43;
}
.todo-tag-2 {
    background: #64d572;
}
.todo-tag-3 {
    background: #2d8cf0;
}
.todo-date {
    color: #929ba7;
    overflow: hidden;
    white-space: nowrap;
}
.todo-item {
    font-size: 14px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
.todo-item > a {
    color: #606266;
}
.todo-item > a:hover {
    text-decoration: underline;
}
.todo-track > i {
    color: #f25e43;
    font-size: 16px;
    cursor: pointer;
}
.todo-delete > i {
    color: #c2cbd6;
    font-weight: bold;
    cursor: pointer;
}
.todo-delete > i:hover {
    color: #606266;
}
.dashboard-content{
    height: 100%;
}
.dashboard-content .el-card{
    border: none;
}
/*.el-tabs__content {
    height:78%;
}*/
.el-tab-pane{
    height:100%;
}
.tabs.el-tabs.el-tabs--top{
    height: 100%;
}

</style>

<style>
.grid-item {
    overflow: hidden;
    border-radius: 4px;
    box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.2);
}
.grid-item2{
    overflow: hidden;
    border-radius: 10px;
    border:1px solid #ddd;
    box-shadow:none;
    background: #fff;
}
.card-box .btn-more{
      position: absolute;
      top:5px;
      right:10px;
      font-size: 12px;
      font-weight:400;
}
i.box-title-icon{
    position: absolute;
    top: 6px;
    left: 12px;
    font-size: 28px;
}
.card-title{
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    font-weight: bold;
    border-bottom: 1px solid #ddd;
    box-sizing: border-box;
    padding-left: 24px;
    color: #333;
    margin-bottom:6px;
  }
  .card-title .btn-more{
    float: right; 
    padding: 7px 5px;
    color:#888888;
    font-size: 12px;
  }
 
</style>
